<template>
  <div v-loading="loading">
    <tableComponents
      :pagination="{currentPage,pageSize,total:list.all_num}"
      @handleCurrentChange="handleCurrentChange"
      @handleSizeChange="handleSizeChange"
      :isShow="false"
      :isHaveExport="false"
      @toAdd="checkWar"
      addText="结算赛季"
      addIcon="el-icon-eleme"
    >
      <template v-slot:table>
        <el-table
          ref="multipleTable"
          :data="list.data"
          stripe
          header-row-class-name="head_color"
          tooltip-effect="dark"
          style="width: 99.8%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" class="tc" width="35"></el-table-column>

          <el-table-column label="赛季编号" min-width="35">
            <template slot-scope="scope">{{scope.row.war}}</template>
          </el-table-column>

          <el-table-column label="赛季时间" min-width="50">
            <template
              slot-scope="scope"
            >{{ scope.row.begin_time | getTimeFormat2 }} --- {{ scope.row.end_time |getTimeFormat2}}</template>
          </el-table-column>

          <el-table-column label="排名" min-width="35">
            <template slot-scope="scope">
              <el-button
                :loading="$store.state.isLoading"
                size="mini"
                type="primary"
                @click="endGuess(scope.row.id)"
              >查看排名</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </tableComponents>

    <el-dialog
      :close-on-click-modal="false"
      :title="'结果'"
      :visible.sync="dioForm"
      width="70%"
      center
    >
      <el-table
        ref="multipleTable"
        :data="list2"
        stripe
        header-row-class-name="head_color"
        tooltip-effect="dark"
        style="width: 99.8%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column label="会员" min-width="80" align="center">
          <template slot-scope="scope">
            <div class="tc">
              <img
                style="width:50px;height:50px"
                :src=" imgError(scope.row.avatar) "
                class="user_img"
              />
              <span>{{scope.row.nickname}}</span>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="账号" min-width="50">
          <template slot-scope="scope">
            {{scope.row.username}}
            <br />
          </template>
        </el-table-column>

        <el-table-column label="奖项" min-width="50">
          <template slot-scope="scope">
            {{scope.row.ranking}}
            <br />
          </template>
        </el-table-column>

        <el-table-column label="参与次数" min-width="50">
          <template slot-scope="scope">
            {{scope.row.join_num}}
            <br />
          </template>
        </el-table-column>

        <el-table-column label="猜中次数" min-width="50">
          <template slot-scope="scope">
            {{scope.row.win_num}}
            <br />
          </template>
        </el-table-column>

        <el-table-column label="赛季奖励" min-width="50">
          <template slot-scope="scope">
            {{scope.row.red}} {{scope.row.unit}}
            <br />
          </template>
        </el-table-column>
      </el-table>

      <span slot="footer" class="dialog-footer">
        <el-button
          :loading="$store.state.isLoading"
          size="small"
          v-if="!end"
          type="primary"
          @click="subpoitInfo"
        >确 定</el-button>
      </span>
    </el-dialog>

    <EarlyEdit @parent="reload" :show.sync="myshow" :editId="editId" :clickNum="clickNum"></EarlyEdit>
  </div>
</template>
<script>
import EarlyEdit from "@/components/plugin/early_edit.vue";

export default {
  data() {
    return {
      show: false,
      myshow: false,
      loading: false,
      list: [],
      list2: [],
      allPage: 0,
      allNum: 0,
      pageSize: 10,
      currentPage: 1,
      editId: "",
      multipleSelection: "",
      arr: [],
      visible: false,
      clickNum: 1,
      updataIndex: "",
      searchAll: {},
      dioForm: false,
      form: {
        id: "",
        point: "",
        rf: ""
      },
      end: false
    };
  },
  components: {
    EarlyEdit
  },
  methods: {
    imgError(item) {
      if (!item) {
        return require("../../assets/img/default_head.png");
      }
      if (item.indexOf("http") >= 0) {
        return item;
      } else {
        return "/api/" + item;
      }
    },
    showpoit(row) {
      this.form = row;
      this.dioForm = true;
      this.end = false;
    },
    endGuess(id) {
      //this.form=row;
      this.dioForm = true;
      this.end = true;
      var params = {};
      params.id = id;

      this.$api.getPlugin.getGuessWarInfoList(params).then(res => {
        console.log(res);
        if (res.data.code == 200) {
          this.list2 = res.data.result;
        } else {
          this.$fnc.alertError(res.data.code_desc);
        }
      });
    },

    checkWar() {
      this.$swal
        .fire({
          title: "是否结算?",
          text: "是否结算上赛季",
          type: "warning",
          showCancelButton: true,
          confirmButtonColor: "#3085d6",
          cancelButtonColor: "#d33",
          confirmButtonText: "结算",
          cancelButtonText: "不结算"
        })
        .then(result => {
          if (result.value) {
            var params = {};
            this.$api.getPlugin.guessWar(params).then(res => {
              if (res.data.code == 200) {
                this.$fnc.alertSuccess({ text: "结算成功" });
                this.getNewsAll(this.searchAll);
              } else {
                this.$fnc.alertError(res.data.code_desc, res.data.result);
              }
            });
          }
        });
    },

    JieSuan(id, stage) {
      this.$swal
        .fire({
          title: "是否结算?",
          text: "是否结算期号：" + stage,
          type: "warning",
          showCancelButton: true,
          confirmButtonColor: "#3085d6",
          cancelButtonColor: "#d33",
          confirmButtonText: "结算",
          cancelButtonText: "不结算"
        })
        .then(result => {
          if (result.value) {
            var params = {};
            params.id = id;
            params.stage = stage;
            //  params.rf=rf;
            this.$api.getPlugin.endearly(params).then(res => {
              if (res.data.code == 200) {
                this.$fnc.alertSuccess({ text: "结算成功" });
                this.getNewsAll(this.searchAll);
              } else {
                this.$fnc.alertError(res.data.code_desc, res.data.result);
              }
            });
          }
        });
    },

    subpoitInfo() {
      var params = {};
      if (!this.end) {
        console.log(this.form);
        params.id = this.form.id;
        params.point = this.form.point;
        params.rf = this.form.rf;
        this.$api.getPlugin.setpoitinfo(params).then(res => {
          console.log(res);
          if (res.data.code == 200) {
            this.$fnc.alertSuccess({ text: "录入成功" });
            this.dioForm = false;
          } else {
            this.$fnc.alertError(res.data.code_desc, res.data.result);
          }
        });
      }
    },
    showAddDialog() {
      this.show = true;
      console.log(this.show);
    },
    getSearch(obj) {
      this.searchAll = obj;
      this.getNewsAll(obj);
    },
    getNewsAll(data) {
      this.loading = true;
      var params = {};
      data = data || {};
      params = data;
      this.$api.getPlugin.getGuessWarList(params).then(res => {
        if (res.data.code == 200) {
          this.list = res.data.result;
        }
        this.loading = false;
      });
    },
    handleSizeChange(val) {
      var params = this.searchAll;
      params.page = "";
      params.page_size = val;
      this.getNewsAll(params);
      this.pageSize = val;
      // console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      // console.log(`当前页:`+val)
                this.currentPage = val;
      var params = this.searchAll;
      params.page = val;
      params.page_size = this.pageSize;
      this.getNewsAll(params);
    },
    handleSelectionChange(val) {
      //多选框赋值
      this.multipleSelection = val;
    },
    delSelection() {
      //删除
      this.$swal({
        title: "您确定要删除所选信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思"
      }).then(willDelete => {
        if (willDelete.value) {
          var str = "";
          for (var i in this.multipleSelection) {
            console.log(this.multipleSelection[i]["id"]);
            str += this.multipleSelection[i].id + "@";
          }
          str = str.slice(0, -1);
          console.log(str);
          var data = { id_str: str };
          this.$api.getPlugin.delearly(data).then(res => {
            if (res.data.code == 200) {
              this.$swal("删除成功！", "您已经删除了所选信息。", "success");
              this.getNewsAll(this.searchAll);
            } else {
              this.$swal("删除失败！", "出错啦！", "success");
            }
          });
        } else if (willDelete.dismiss == "cancel") {
          this.$swal("已取消", "您取消了删除操作", "error");
        }
      });
    },
    deleteNew(index, id) {
      console.log(id);
      this.$swal({
        title: "您确定要删除这条信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思"
      }).then(willDelete => {
        if (willDelete.value) {
          var data = { id_str: id };
          this.$api.getPlugin.delearly(data).then(res => {
            if (res.data.code == 200) {
              this.$swal("删除成功！", "您已经删除了这条信息。", "success");
              this.getNewsAll(this.searchAll);
            } else {
              this.$swal("删除失败！", "出错啦！", "success");
            }
          });
        } else if (willDelete.dismiss == "cancel") {
          this.$swal("已取消", "您取消了删除操作", "error");
        }
      });
    },
    showEditDialog(id) {
      this.myshow = true;
      this.editId = id;
      this.clickNum++;
      console.log(this.clickNum);
    },
    reload(id) {
      this.getNewsAll(this.searchAll);
    }
  },
  beforeCreate() {
    this.loading = true; //数据未加载之前调用loading
  },
  created() {
    this.getNewsAll(this.searchAll);
    this.loading = false;
  },
  computed: {},
  watch: {
    arr() {
      console.log("批量操作", this.arr);
    }
  }
};
</script>

<style lang="less" scoped>
thead > tr > th {
  height: 45px;
  line-height: 45px;
}
td,
th {
  border: 1px solid #e1e3e4;
  font-size: 12px;
}
th {
  background: #f8f8f8;
  font-weight: normal;
  color: #323437;
}
td {
  height: 75px;
  color: #000;
}
.caozuo {
  padding-left: 50px;
}
.pl {
  padding-left: 15px;
}
.chkBox {
  display: block;
  margin: 0 auto;
  width: 18px;
  height: 18px;
  background: #fff;
}
.aBtn_1 {
  padding: 10px;
}
</style>
